﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmTimKiemBangKeNhapHang : Form
    {
        private BangKeNhapHangMgt bangkenhaphangMgt = new BangKeNhapHangMgt();
        private CTBangKeNhapHangMgt ctbangkenhaphangMgt = new CTBangKeNhapHangMgt();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemBangKeNhapHang(NhanVien nv)
        {
            InitializeComponent();
            this.NhanVien = nv;            
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            ResultObj result = new ResultObj();

            result = bangkenhaphangMgt.GetAllBangKeNhapHang();
            List<BangKeNhapHang> lstBangKeNhapHang = (from c in (List<BangKeNhapHang>)result.Result
                                                      where (c.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                            c.NhaCungCap.Contains(txtTimKiem.Text)) &&
                                                            (c.NgayThang.Date >= dtTuNgay.Date && c.NgayThang.Date <= dtDenNgay.Date)&&
                                                            c.IsDelete==0
                                                      select c).ToList();

            dgvBangKeNhapHang.AutoGenerateColumns = false;
            dgvBangKeNhapHang.DataSource = lstBangKeNhapHang;
        }

        private void dgvBangKeNhapHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BangKeNhapHang bangkenhaphang = (BangKeNhapHang)dgvBangKeNhapHang.CurrentRow.DataBoundItem;

            ResultObj result = new ResultObj();
            result = bangkenhaphangMgt.GetAllBangKeNhapHang();

            List<BangKeNhapHang> lstBangKeNhapHang = (from c in (List<BangKeNhapHang>)result.Result
                                                      where c.Id == bangkenhaphang.Id
                                                      select c).ToList();

            FrmNhapHang frmNhapHang = new FrmNhapHang(bangkenhaphang, nhanvien);
            this.Hide();
            frmNhapHang.ShowDialog();
            this.Show();
            dgvBangKeNhapHang.DataSource = null;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmNhapHang frmNhapHang = new FrmNhapHang(this.NhanVien);
            this.Hide();
            frmNhapHang.ShowDialog();
            this.Show();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
